
package view;

import controllers.ReportHistoryController;
import java.util.List;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import vo.ReportVO;

public class ReportHistory extends GridPane {
    
    private final ObservableList<ReportVO> reports;
    private static Task getReports;
    
    //<editor-fold defaultstate="collapsed" desc="Table Nodes">
    private final TableView historyTable;
    private TableColumn<ReportVO,String> issueDateCol;
    private TableColumn<ReportVO,String> locationCol;
    private TableColumn<ReportVO,String> typeCol;
    private TableColumn<ReportVO,String> franchiseCol;
    private TableColumn<ReportVO,String> branchCol;
    private TableColumn<ReportVO,String> startDateCol;
    private TableColumn<ReportVO,String> endDateCol;
    private TableColumn<ReportVO,String> amountCol;
    //</editor-fold>
            
    public ReportHistory(Stage primaryStage) {
        prefWidthProperty().bind(primaryStage.widthProperty().subtract(16));
        prefHeightProperty().bind(primaryStage.heightProperty().subtract(38));
        setPadding(new Insets(50, 30, 30, 30));
        reports=FXCollections.observableArrayList();
        historyTable=new TableView(reports);
        historyTable.setPlaceholder(new Label("Searching..."));
        historyTable.prefHeightProperty().bind(heightProperty().subtract(getPadding().getTop()).subtract(getPadding().getBottom()+20));
        historyTable.prefWidthProperty().bind(widthProperty().subtract(getPadding().getRight()).subtract(getPadding().getRight()));
        
        issueDateCol=initCol("Fecha reporte", "issueDate", 0.1);
        typeCol=initCol("Tipo", "type", 0.1);
        franchiseCol=initCol("Franquicia", "franchise", 0.1);
        branchCol=initCol("Sucursal", "branch", 0.1);
        startDateCol=initCol("Fecha inicio", "startDate", 0.1);
        endDateCol=initCol("Fecha fin", "endDate", 0.1);
        amountCol=initCol("Monto", "amount", 0.1);
        locationCol=initCol("Ubicación", "location", 0.3);
        
        historyTable.getColumns().addAll(issueDateCol,typeCol,franchiseCol,branchCol,startDateCol,endDateCol,amountCol,locationCol);
        getChildren().setAll(historyTable);
    }
    
    private TableColumn<ReportVO,String> initCol(String title, String property, double width){
        TableColumn<ReportVO,String> col=new TableColumn<>(title);
        col.setCellValueFactory(new PropertyValueFactory(property));
        col.prefWidthProperty().bind(historyTable.prefWidthProperty().multiply(width));
        return col;
    }
    
    private Task getReposrtList(){
        return new Task<List<ReportVO>>() {
            @Override
            protected List<ReportVO> call() throws Exception {
                List<ReportVO> reportHistory = ReportHistoryController.getReportHistory();
                return reportHistory;
            }
        };
    }
    
    protected void fillTable() {
        getReports=getReposrtList();
        getReports.valueProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue arg0, Object arg1, Object arg2) {
                Object reportsResult=getReports.getValue();
                if(reportsResult==null)
                    historyTable.setPlaceholder(new Label("Ocurrio un error al consultar la base de datos"));
                List<ReportVO> reportsVO=(List<ReportVO>)reportsResult;
                if(reportsVO.isEmpty())
                    historyTable.setPlaceholder(new Label("No se encontraron registros de reportes"));
                else
                    reports.setAll(reportsVO);
            }
        });
        Thread getReportsThread=new Thread(getReports);
        getReportsThread.setDaemon(true);
        getReportsThread.start();
    }
    
}